package com.iss.ljx.role.controller;

import com.iss.ljx.mybatis.entity.PtRoleEntity;
import com.iss.ljx.mybatis.flexigrid.PadingRstType;
import com.iss.ljx.mybatis.flexigrid.PagingBean;
import com.iss.ljx.mybatis.ztree.ZtreeEntity;
import com.iss.ljx.role.entity.SearchRoleEntity;
import com.iss.ljx.role.service.RoleService;
import com.iss.ljx.spring.controller.BaseController;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {
    private Logger logger = LogManager.getLogger(this.getClass());
    @Resource
    private RoleService roleService;
    @Secured("ROLE_gqgy_data_role")
    @RequestMapping("/rolePage")
    public String rolePage(){
        return "role/rolePage";
    }
    @Secured("ROLE_gqgy_data_role")
    @RequestMapping("/queryRoleListPage")
    @ResponseBody
    public PadingRstType<PtRoleEntity> queryRoleListPage(SearchRoleEntity search, PagingBean paging){
        PadingRstType<PtRoleEntity> ptRoleEntityPadingRstType = roleService.queryRoleListPage(search,paging);
        return ptRoleEntityPadingRstType;
    }
    @Secured("ROLE_gqgy_data_role")
    @RequestMapping("/getMenuByRoleId")
    @ResponseBody
    public List<ZtreeEntity> getMenuByRoleId(@RequestParam("roleUuid") String roleUuid){
        List<ZtreeEntity> ztreeStrEntities = roleService.queryMenuByRoleId(roleUuid);
        return ztreeStrEntities;
    }

    @Secured("ROLE_gqgy_data_role")
    @RequestMapping("/saveMenuIdAndRoleId")
    @ResponseBody
    public String saveMenuIdAndRoleId(@RequestParam("roleUuid") String roleUuid,
                                      @RequestParam("menuArray") String menuArray){
        roleService.saveMenuIdAndRoleId(roleUuid,menuArray);
        return getSuccessMsg("role.menu.allocate.success");
    }

   // @Secured("")
    @RequestMapping("roleAdd")
    @ResponseBody
    public String roleAdd(PtRoleEntity ptRoleEntity){
        roleService.roleAdd(ptRoleEntity);
        return getSuccessMsg("role.add.success");
    }

   // @Secured("")
    @RequestMapping("roleUpdate")
    @ResponseBody
    public String roleUpdate(PtRoleEntity ptRoleEntity){
        roleService.roleUpdate(ptRoleEntity);
        return getSuccessMsg("role.update.success");
    }

    // @Secured("")
    @RequestMapping("roleDelete")
    @ResponseBody
    public String roleDelete(PtRoleEntity ptRoleEntity){
        roleService.roleDelete(ptRoleEntity);
        return getSuccessMsg("role.del.success");
    }

    @RequestMapping("/saveUserIdAndRoleId")
    @ResponseBody
    public String saveUserIdAndRoleId(@RequestParam("userUuid") String userUuid,
                                      @RequestParam("roleArray") String roleArray){
        roleService.saveUserIdAndRoleId(userUuid,roleArray);
        return getSuccessMsg("user.allocat.role.success");
    }
}
